package in.muton.edushare.newentities;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.edushare.common.dao.UsersDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

public class UserInviter implements Serializable {
	private static final long serialVersionUID = 3924397754379697711L;
	ORMInterface ormInterface;

	public UserInviter(ORMInterface ormInterface) {
		this.ormInterface = ormInterface;
	}

	public static String[] breakString(String userList) {
		Pattern p = Pattern.compile(";+");
		userList = userList.replace(' ', ';').replace(',', ';').replace('\n',
				';').replace('\r', ';');
		Matcher m = p.matcher(userList);
		return m.replaceAll(";").split(";");
	}

	public static boolean isEmailId(String id) {
		if(!StringUtil.isValid(id))
			return false;
		
		try {
			new javax.mail.internet.InternetAddress(id, true);
		} catch (javax.mail.internet.AddressException e) {
			return false;
		}
		return true;
	}

	public Users getUserByString(String emailorid) {
		Users userVO = new UsersDAO(ormInterface).getUserByUserNameOrEmail(emailorid);
		return userVO;
	}

	public boolean isUnivSubscriber(Universities univ, Users user) {
		List<UserRoles> roleList = user.getRoleList();
		for (UserRoles role : roleList) {
			if (univ == role.getUniversity()) {
				return true;
			}
		}
		return false;
	}

	public int getMatchingUnivRole(int role) {
		switch (role) {
		case (UserDeptLink.PRIMARY_ADMIN):
			return UserRoles.TEACHER_ROLE;
		case (UserDeptLink.TEACHER):
			return UserRoles.TEACHER_ROLE;
		case (UserDeptLink.STUDENT):
			return UserRoles.STUDENT_ROLE;
		}
		return 0;
	}

	
	
	
}
